/**
 * 上传商品
 */
myApp_.onPageInit('seller_supply', function () {
    SUPPLY_GOODS.do_get();
    SUPPLY_GOODS.request_select();
});


SUPPLY_GOODS = {}


SUPPLY_GOODS.__INIT__ = function (goods_id) {
    SUPPLY_GOODS.goods_id = goods_id;
    SUPPLY_GOODS.jump();
}

SUPPLY_GOODS.jump = function () {
    mainView.router.load({
        url: 'src/seller/goods/supply.html',
    });
}
//初始执行请求
SUPPLY_GOODS.do_get = function () {
    var goods_id = SUPPLY_GOODS.goods_id;
    request_(
        url_('addons/execute/seller-App-get_goods_info'),
        {goods_id: goods_id},
        function (d) {
            if (d.code == 0 && d.data.goods_info) {
                SUPPLY_GOODS.fill_form(d.data.goods_info);
            }
        }
    );
}
SUPPLY_GOODS.goods_image_to_img = function (src) {
    src = src.split(";");
    for (i in src) {
        $("#camera_img").append("<img src='" + img_url + '/' + src[i] + "' data-img_src='" + src[i] + "' onclick='SUPPLY_GOODS.del_img(this)' width='50%'/>");
    }
}
SUPPLY_GOODS.src_to_img = function (src) {
    $("#camera_img").append("<img src='" + img_url + '/' + src + "' data-img_src='" + src + "' width='50%'/>");
}
SUPPLY_GOODS.img_to_src = function () {
    var src = [];
    $("#camera_img img").each(function () {
        src.push($(this).data("img_src"));
    });
    return src = src.join(';');
}
SUPPLY_GOODS.del_img = function (obj) {
   myApp_.confirm("删除此图片？",function(){
       $(obj).remove();
   },function(){
       return false;
   });
}

SUPPLY_GOODS.fill_form = function (goods,goods_category) {
    $("input[name='goods_id']").val(goods.id);
    $("input[name='goods_name']").val(goods.goods_name);
    $("input[name='place_of_origin']").val(goods.place_of_origin);
    $("input[name='image']").val(goods.image);
    $("input[name='purchase_price']").val(goods.purchase_price);
    $("input[name='price']").val(goods.price);
    $("input[name='stock']").val(goods.stock);
    $("input[name='weight']").val(goods.weight);
    $("input[name='seo']").val(goods.seo);
    $("textarea[name='content']").val(goods.content);
    $("input[name='bar_code']").val(goods.bar_code);
    SUPPLY_GOODS.goods_image_to_img(goods.image);
    
}

SUPPLY_GOODS.request_select = function(){
	request_(
		url_('addons/execute/seller-App-get_goods_category')
		,{
			goods_id: SUPPLY_GOODS.goods_id?SUPPLY_GOODS.goods_id:''
		}
		,function(d){
			if (d.code == 0) {
                SUPPLY_GOODS.fill_select(d.data);
            }
		}
	);
	
}

SUPPLY_GOODS.fill_select = function(data){
	var fill_cate = function(category,elem,cb){
		var h = '<option value="-1">请选择</option>'
    	for(var i in category){
			if(category[i].is_checked){
				h += "<option data-son='"+JSON.stringify(category[i].son)+"' selected value='"+category[i].id+"'>"+category[i].category_name+"</option>";
	    		if(category[i].son&&cb){
	    			cb(category[i].son);
	    		}
			}else{
				h += "<option data-son='"+JSON.stringify(category[i].son)+"'  value='"+category[i].id+"'>"+category[i].category_name+"</option>";
	    	}
		}
    	$(elem).html(h);
    }
    
    //第一级分类
    fill_cate(data,'#category_1',function(son_data2){
    //第二级分类
    	$('#category_2').css("display","block");
    	fill_cate(son_data2,'#category_2',function(son_data3){
    //第三级分类
    		$('#category_3').css("display","block");
    		fill_cate(son_data3,'#category_3');
    	})
    });
}

SUPPLY_GOODS.change_select = function(e){
	var son = $(e).find('option:checked').data('son');
	if (son != "undefined") {
		$(e).next().html('<option value="-1">请选择</option>');
		for (var i in son) {
	        $(e).next().append("<option data-son='"+JSON.stringify(son[i].son)+"' value='"+son[i].id+"'>"+son[i].category_name+"</option>")
    	}
		//联动
		$(e).next().show().next().html('').hide();
		$(e).next().css("display","block");
	}else{
		$(e).next().html('').hide().next().html('').hide();
	}
}

SUPPLY_GOODS.submit_form = function (goods) {
    var goods_id = $("input[name='goods_id']").val();
    var image = SUPPLY_GOODS.img_to_src();
    $("input[name='image']").val(image);
    var category_1 = $("#category_1 option:checked").val();
    var category_2 = $("#category_2 option:checked").val();
    var category_3 = $("#category_3 option:checked").val();
    var goods_category = [category_1, category_2, category_3];
    
    var param = {
        goods_id: goods_id > 0 ? goods_id : '',
        goods_name: $("input[name='goods_name']").val(),
        //place_of_origin: $("input[name='place_of_origin']").val(),
        image: $("input[name='image']").val(),
        price: $("input[name='price']").val(),
        stock: $("input[name='stock']").val(),
        weight: $("input[name='weight']").val(),
        goods_category:goods_category,
        content: $("textarea[name='content']").val(),
    };
    request_(
        url_('addons/execute/seller-App-edit_goods'),
        param,
        function (d) {
            if (d.code == 0) {
                go_("seller/goods/list");
            }
            my_tips_(d.msg)
        }
    );
}

SUPPLY_GOODS.camera = function () {
    use("tool/camera");
    $.camera({
        callback: function (param) {
            var data = param.response;
            if (data.code == 0) {
                var img = data.data;
                SUPPLY_GOODS.src_to_img(img)
            }
        }
    });
}
